(in-package :yadd)

(deftestsuite make-job-test ()  
  ()  
  (:test ((ensure (equal nil (make-job nil)))))
  (:test ((ensure (equal nil (make-job "!@#*")))))
  (:test ((ensure-same
           (class-name (class-of (make-job "http://dsv.data.cod.ru/123456")))
           'DSV.DATA.COD.RU-JOB)))
  (:test ((ensure-same
           (class-name (class-of (make-job "http://dsvload.net/ftpupload/SomeFolder/Some file.rar")))
           'DSVLOAD.NET-JOB)))
  (:test ((ensure-same
           (class-name (class-of (make-job "http://77.35.112.82/SomeFolder/Some file.rar")))
           '77.35.112.8?-JOB)))           
  (:test ((ensure-same
           (class-name (class-of (make-job "http://data.cod.ru/12345")))
           'DATA.COD.RU-JOB))))

(deftestsuite job-name-test ()
  ()
  (:test ((ensure-same (job-name (make-job "http://77.35.112.82/SomeFolder/Некоторый файл.rar"))
                       "Некоторый файл.rar"))))

;; DRAKMA testing

;; ICONV test

;; (mapcar #'(lambda (fr to) (iconvert fr to avatara-name))
;;         '("utf8" "utf8" "utf-8" "koi8" "koi8" "koi8" "cp1251" "cp1251" "cp1251")
;;         '("utf8" "cp1251" "koi8-r" "koi8" "cp1251" "utf8" "koi8" "cp1251" "utf8"))
;; (string->vector "Á×ÁÔÁÒÙ.rar")

;; (coerce "Ð°Ð²Ð°ÑÐ°ÑÑ.rar" '(unsigned-byte 8)))

;; (defmethod job-name-BASED-ON-HEADER ((job common-data.cod.ru-job))
;;   ;; Problems with iconv
;;   (with-slots (name) job
;;     (or name
;;       (let ((new-name
;;         (when-let ((link-header (header (job-link job))))
;;           (header-parameter link-header :CONTENT-DISPOSITION :FILENAME))))
;;         (when new-name (setf name new-name))))))

;; (defparameter job1 (first (make-jobs)))
;; (mapcar (lambda (x) (funcall x job1))
;;         '(job-link
;;           job-name
;;           job-file
;;           job-file-length))

;; (defparameter job2 (fourth (make-jobs)))
;; (mapcar (lambda (x) (funcall x job2))
;;         '(job-link
;;           job-name
;;           job-file
;;           job-file-length))

;; (let ((x (format t "asd: ~a~%" 2)))
;;   x)


(string #U{"http://77.35.112.82/upload/Personal_Folders/RastoLife/Fate. Stay Night/Fate._Stay_Night_[01_of_24]_[ru_jp_en]_[Cuba77_&_AnimeReactor_Ru].mkv"})

(uri-path "http://77.35.112.82/upload/Personal_Folders/RastoLife/Fate. Stay Night/Fate._Stay_Night_[01_of_24]_[ru_jp_en]_[Cuba77_&_AnimeReactor_Ru].mkv")

(defun encode-uri (uri)
  (bind-standard-uri-components uri
    (str %uri-scheme "://" %uri-authority "/"
         (apply #'str
                (interpose "/" 
                           (mapcar (lambda (x)
                                     (kmrcl:encode-uri-string (princ-to-string x)))
                                   (remove-if #'empty? (split "/" %uri-path))))))))


(encode-uri "http://77.35.112.82/upload/Personal_Folders/RastoLife/Fate. Stay Night/Comment.txt")
(http-request "http://77.35.112.82/upload/Personal%5FFolders/RastoLife/Fate%2E%20Stay%20Night/Comment%2Etxt")

;;(defun main ()
;;  (multithreaded-download (make-jobs)))

;; (sb-ext:save-lisp-and-die "/tmp/mcclim-listener-0.9.2/mcclim"
;;                           :executable t
;;                           :toplevel (lambda ()
;;                                       (clim-listener:run-listener)
;;                                       (sb-ext:quit :unix-status 0)))
